package com.zyao89.view.zloading.circle;

import com.zyao89.view.zloading.ZLoadingBuilder;
import com.zyao89.view.zloading.util.NumCalcUtil;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.ColorFilter;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

/**
 * Created by zyao89 on 2017/3/23.
 * Contact me at 305161066@qq.com or zyao89@gmail.com
 * For more projects: https://github.com/zyao89
 * My Blog: https://zyao89.cn
 */
public class DoubleCircleBuilder extends ZLoadingBuilder {
    private static final int OUTER_CIRCLE_ANGLE = 270;
    private static final int INTER_CIRCLE_ANGLE = 90;
    private Paint mStrokePaint;
    private RectFloat mOuterCircleRectF;

    private RectFloat mInnerCircleRectF;
    private Arc mRotateArc, mInnerArc;
    //旋转角度
    private int mRotateAngle;


    @Override
    protected void initParams(Context context, Color color) {
        //最大尺寸
        float outR = getAllSize();
        //小圆尺寸
        float inR = outR * 0.6f;
        //初始化画笔
        initPaint(inR * 0.4f, color);
        //旋转角度
        mRotateAngle = 0;
        //圆范围
        mOuterCircleRectF = new RectFloat(NumCalcUtil.subtract(getViewCenterX(), outR), NumCalcUtil.subtract(getViewCenterY(), outR), NumCalcUtil.add(getViewCenterX(), outR), NumCalcUtil.add(getViewCenterY(), outR));
        mInnerCircleRectF = new RectFloat(NumCalcUtil.subtract(getViewCenterX(), inR), NumCalcUtil.subtract(getViewCenterY(), inR), NumCalcUtil.add(getViewCenterX(), inR), NumCalcUtil.add(getViewCenterY(), inR));
        mRotateArc = new Arc();
        mInnerArc = new Arc();
    }

    /**
     * 初始化画笔
     *
     * @param lineWidth 画笔线宽
     * @param color 画笔颜色
     */
    private void initPaint(float lineWidth, Color color) {
        mStrokePaint = new Paint();
        mStrokePaint.setAntiAlias(true);
        mStrokePaint.setStyle(Paint.Style.STROKE_STYLE);
        mStrokePaint.setStrokeWidth(lineWidth);
        mStrokePaint.setColor(color);
        mStrokePaint.setDither(true);
        mStrokePaint.setFilterBitmap(true);
        mStrokePaint.setStrokeCap(Paint.StrokeCap.ROUND_CAP);
        mStrokePaint.setStrokeJoin(Paint.Join.ROUND_JOIN);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();
        //外圆
        mRotateArc.setArc(mRotateAngle % 360, OUTER_CIRCLE_ANGLE, false);
        canvas.drawArc(mOuterCircleRectF, mRotateArc, mStrokePaint);
        //内圆
        mInnerArc.setArc(270 - mRotateAngle % 360, INTER_CIRCLE_ANGLE, false);
        canvas.drawArc(mInnerCircleRectF, mInnerArc, mStrokePaint);
        canvas.restore();
    }

    @Override
    protected void setAlpha(int alpha) {
        mStrokePaint.setAlpha(alpha);
    }

    @Override
    protected void prepareStart(AnimatorValue floatValueAnimator) {

    }

    @Override
    protected void prepareEnd() {

    }

    @Override
    protected void computeUpdateValue(AnimatorValue animation, float animatedValue) {
        mRotateAngle = (int) (360 * animatedValue);
    }

    @Override
    protected void setColorFilter(ColorFilter colorFilter) {
        mStrokePaint.setColorFilter(colorFilter);
    }


    @Override
    public void onRepeat(Animator animator) {

    }

}
